﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using Indeco.G24.Web;
using Indeco.G24.Data;
using Indeco.G24.Services;
using Indeco.G24.Entities;
using DevExpress.Web.ASPxGridView;

namespace Indeco.G24.Website
{
    public partial class Home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblCulture.Text = Session[Global.SESSION_KEY_CULTURE].ToString();
            lblG24SessionId.Text = Session["G24SessionId"].ToString();
            lblIDNhaCungCap.Text = Session["IDNhaCungCap"].ToString();
            lblIDNguoiDung.Text = Session["IDNguoiDung"].ToString();
            lblIDDiaBan.Text = Session["IDDiaBan"].ToString();
            lblUserOnline.Text = Application["UserOnline"].ToString();

            //try
            //{
            //    //Add control vao Tab Chung tay xay dung
            //    Control UserControlRSSBlog = this.LoadControl("~/UserControls/RssReader.ascx");
            //    UserControls.RssReader rssReader = (UserControls.RssReader)UserControlRSSBlog;
            //    rssReader.RssUrl = "http://gia24.vn/chungtayxaydung/blogs/MainFeed.aspx";
            //    rssReader.RssMaxItems = 10;
            //    //ASPxPageControlRssChungtayxaydung.TabPages[1].Controls.Add(rssReader);
            //    //Control UserControlRSSForum = this.LoadControl("~/UserControl/RssReader.ascx");
            //    //rssReader = (UserControl.RssReader)UserControlRSSForum;
            //    //rssReader.RssUrl = "http://gia24.vn/chungtayxaydung/forums/rss.aspx?ForumID=101&Mode=0";
            //    //rssReader.RssMaxItems = 10;
            //    //ASPxPageControlRssChungtayxaydung.TabPages[0].Controls.Add(rssReader);
            //    Control TabChungtayxaydung = this.LoadControl("~/UserControls/TabChungtayxaydung.ascx");
            //    PanelForumInfo.Controls.Add(TabChungtayxaydung);
            //    //WebPartZoneLeftMiddle.Page.Controls.Add(TabChungtayxaydung);
            //}
            //catch
            //{ }
            //NavBar 
            if (IsCallback)
            {
                System.Threading.Thread.Sleep(1000); // Intentionally pauses server-side processing, to demonstrate the Loading Panel functionality.
                ASPxGridView bangGiaSanPhamActive = (ASPxGridView)NavBarDiaBanThucThi.FindControl("BangGiaSanPhamActive");
                try
                {
                    bangGiaSanPhamActive.GroupBy((GridViewDataColumn)bangGiaSanPhamActive.Columns[0]);
                    bangGiaSanPhamActive.GroupBy((GridViewDataColumn)bangGiaSanPhamActive.Columns[1]);
                }
                catch
                { }
            }
            if (!IsPostBack)
            {
                NavBarDiaBanThucThi.DataBind();
                NavBarDiaBanThucThi.Groups.CollapseAll();
                NavBarDiaBanThucThi.Groups[0].Expanded = true;
            }
            else
            {
                ASPxGridView bangGiaSanPhamActive = (ASPxGridView)NavBarDiaBanThucThi.FindControl("BangGiaSanPhamActive");
                try
                {
                    bangGiaSanPhamActive.GroupBy((GridViewDataColumn)bangGiaSanPhamActive.Columns[0]);
                    bangGiaSanPhamActive.GroupBy((GridViewDataColumn)bangGiaSanPhamActive.Columns[1]);
                    bangGiaSanPhamActive.ExpandAll();
                }
                catch
                { }

                NavBarDiaBanThucThi.DataBind();
            }
            try
            {
                NguoiDungService nguoiDungService = new NguoiDungService();
                TList<NguoiDung> nguoiDung = nguoiDungService.Find("TenSuDung = '" + Membership.GetUser().UserName + "'");
                Session["IDNguoiDung"] = nguoiDung[0].IDNguoiDung;
                Label1.Text = Session["IDNguoiDung"].ToString();
                NguoiDungNhaCungCapService nguoiDungNhaCungCapService = new NguoiDungNhaCungCapService();
                TList<NguoiDungNhaCungCap> listNN = nguoiDungNhaCungCapService.GetByIDNguoiDung(new Guid(Session["IDNguoiDung"].ToString()));
                Session["IDNhaCungCap"] = listNN[0].IDNhaCungCap;
                if (Session["IDNhaCungCap"].ToString() != Guid.Empty.ToString())
                {
                    HyperLink link = (HyperLink)loginView.FindControl("linkControlPanel");
                    link.Visible = true;
                }
            }
            catch
            { }


            //generalSiteMap();

        }
        protected void ASPxNavBar1_GroupDataBound(object source, DevExpress.Web.ASPxNavBar.NavBarGroupEventArgs e)
        {
            IHierarchyData hierarchyData = (e.Group.DataItem as IHierarchyData);
            ASPxGridView bangGiaSanPhamActive = (ASPxGridView)NavBarDiaBanThucThi.FindControl("BangGiaSanPhamActive");
            try
            {
                bangGiaSanPhamActive.GroupBy((GridViewDataColumn)bangGiaSanPhamActive.Columns[0]);
                bangGiaSanPhamActive.GroupBy((GridViewDataColumn)bangGiaSanPhamActive.Columns[1]);
            }
            catch
            { }
            //XmlElement xmlElement = hierarchyData.Item as XmlElement;
            //XmlAttributeCollection attributes = xmlElement.Attributes;

            //if (xmlElement.Attributes["View"] != null)
            //e.Group.Text = "Vu Huy Tung";// xmlElement.Attributes["View"].Value;
        }


        protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            Session["IDNhaCungCap"] = Guid.Empty;
            Session["IDNguoiDung"] = Guid.Empty;
            Session["TenSuDung"] = "";
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            try
            {
                NguoiDungService nguoiDungService = new NguoiDungService();
                TList<NguoiDung> nguoiDung = nguoiDungService.Find("TenSuDung = '" + Membership.GetUser().UserName + "'");
                Session["IDNguoiDung"] = nguoiDung[0].IDNguoiDung;
                Label1.Text = Session["IDNguoiDung"].ToString();
                NguoiDungNhaCungCapService nguoiDungNhaCungCapService = new NguoiDungNhaCungCapService();
                TList<NguoiDungNhaCungCap> listNN = nguoiDungNhaCungCapService.GetByIDNguoiDung(new Guid(Session["IDNguoiDung"].ToString()));
                Session["IDNhaCungCap"] = listNN[0].IDNhaCungCap;
            }
            catch
            { }
        }

        /// <summary>
        /// Test SideMap
        /// </summary>
        private string _siteMapFileName = "SiteMap/ProvinceActive.sitemap";
        private const String SiteMapNodeName = "siteMapNode";
        private void loadFolder(XmlElement rootElement)
        {
            TList<TinTucDanhMuc> danhmuccha = DataRepository.TinTucDanhMucProvider.Find("id_tintucdanhmuccha='" + Guid.Empty.ToString() + "'");
            if (danhmuccha.Count > 0)
            {
                foreach (TinTucDanhMuc dm in danhmuccha)
                {
                    XmlElement teams = AddDynamicChildElement(rootElement, "~/News/Category.aspx?IDTinTucDanhMuc=" + dm.IDTinTucDanhMuc.ToString(), dm.Ten, "");
                    TList<TinTucDanhMuc> danhmucon = DataRepository.TinTucDanhMucProvider.Find("id_tintucdanhmuccha='" + dm.IDTinTucDanhMuc + "'");
                    if (danhmucon.Count > 0)
                    {
                        foreach (TinTucDanhMuc dmc in danhmucon)
                        {
                            TList<Indeco.G24.Entities.TinTuc> tin = DataRepository.TinTucProvider.Find("id_tintucdanhmuc='" + dmc.IDTinTucDanhMuc.ToString() + "'");
                            if (tin.Count > 0)
                            {
                                foreach (Indeco.G24.Entities.TinTuc t in tin)
                                {
                                    AddDynamicChildElement(teams, "~/News/Category.aspx?IDTinTucDanhMuc=" + dmc.IDTinTucDanhMuc.ToString() + "&IDTinTuc=" + t.IDTinTuc.ToString() + "", dmc.Ten, "");
                                }
                            }

                        }
                    }
                }
            }
        }
        public void generalSiteMap()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + _siteMapFileName;
            XmlDocument siteMapXml = new XmlDocument();
            siteMapXml.Load(AppDomain.CurrentDomain.BaseDirectory + _siteMapFileName);
            XmlElement rootElement = (XmlElement)siteMapXml.GetElementsByTagName(SiteMapNodeName)[0];
            rootElement.RemoveAll();
            rootElement.SetAttribute("url", "~/Price/Default.aspx");
            rootElement.SetAttribute("title", "Bảng giá");
            siteMapXml.Save(path);
            //loadFolder(rootElement);
            //siteMapXml.Save(path);
        }
        private static XmlElement AddDynamicChildElement(XmlElement parentElement, String url, String title, String description)
        {
            // Create new element from the parameters
            XmlElement childElement = parentElement.OwnerDocument.CreateElement(SiteMapNodeName);
            childElement.SetAttribute("url", url);
            childElement.SetAttribute("title", title);
            childElement.SetAttribute("description", description);
            // Add it to the parent
            parentElement.AppendChild(childElement);
            return childElement;
        }

    }
}
